﻿package com.wandian.util
{
	import com.wandian.ApplicationFacade;
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.model.vo.PropVo;
	import com.wandian.util.loader.ImageLoader;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class CLZLoadPerson extends EventDispatcher
	{
		private static var _arrPerson:Array=[];
		private static var _facade:ApplicationFacade=ApplicationFacade.getInstance();

		public function CLZLoadPerson()
		{
			return;
		} 

		public static function personById(personId:String, initIcon:String):BitmapData
		{
//			var bitmapData:BitmapData=GameData.getBitMapDataByCache(personId);
//			if(bitmapData==null){
//				bitmapData=GameData.getBitMapDataByCache(initIcon);
//				if(bitmapData==null){
//					throw Error("initIcon not found");
//				}
//			}
//			return bitmapData;
			var completeImage:Function;
			var personVo:PropVo;
			var imageLoader:ImageLoader;
			completeImage=function(event:Event):void
			{
				var loader:ImageLoader=event.target as ImageLoader;
				var index:int=0;
				while (index < _arrPerson.length)
				{

					if (loader.personId == _arrPerson[index].personId)
					{
						_arrPerson[index].isComplete=true;
						_arrPerson[index].bitmapData=loader.bitmapdata;
						_facade.sendNotification(CLZNoticeName.LOAD_PERSON_COMPLETE_BY_ID, loader.personId);
						break;
					}
					index=index + 1;
				}
				return;
			} 
			;
			var num:int=-1;
			var i:int;
			while (i < _arrPerson.length)
			{

				if (personId == _arrPerson[i].personId)
				{
					if (_arrPerson[i].isComplete)
					{
						return _arrPerson[i].bitmapData;
					}
					num=i;
					break;
				}
				i=(i + 1);
			}
			var BitPerson:Class=GameData.getClass(initIcon);
			var bitmapAllPerson:*=new BitPerson;
			if (num < 0)
			{
				_arrPerson.push({personId: personId, bitmapData: bitmapAllPerson, isInit: true, isLoad: true, isComplete: false});
				personVo=GameData.getPropById(int(GameData.PROP_PCATE_ID_PERSON), int(personId));
				imageLoader=new ImageLoader();
				imageLoader.addEventListener(Event.COMPLETE, completeImage);
				imageLoader.load(GameData.flashUrl + GameData.config["imageUrl"] + "person/max/" + personVo.pframe + ".png", int(personId));
			}
			return bitmapAllPerson;
		} 

	}
}
